﻿using SearchStarPlus.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SearchStarPlus.Controllers
{
    public class HomeController : Controller
    {
        ArticleUIController artCon = new ArticleUIController("http://localhost:3178/", "api/article/");
        List<Article> articles = new List<Article>();
        int arrayindex;
       
        public ActionResult Index()
        {      
            return View();
        }

        public ActionResult Search(string q)
        {
            if (q.Length == 0)
            {
                articles = artCon.Get();
            }
            else
            {
                articles = artCon.SearchArticles(q);
            }
            arrayindex = 0;  // resets it to 0
            ViewBag.Size = articles.Count();
            ViewBag.Index = arrayindex + 1;
            ViewBag.Description = articles[arrayindex].Description;
            ViewBag.SearchResults = articles[arrayindex].Text;
            return PartialView("_SearchArticlePartial", articles);
        }

        public ActionResult Next()
        {
            arrayindex++;
            ViewBag.Index = arrayindex + 1;
            ViewBag.Description = articles[arrayindex].Description;
            ViewBag.SearchResults = articles[arrayindex].Text;
            return PartialView("_SearchArticlePartial", articles);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
